
#include <ttg/utils.h>
#include "SeparatedBodies.h"
#include "InterleavedBodies.h"

using namespace ttg::utils;

//Manager, that keeps separated and interleaved versions of bodies
//and performs its synchronization.
class BodyManager
{
	private:
		ParameterInt size;
		ParameterEnum distribution_type;
		Parameter<bool> should_reset;
		SeparatedBodies sbodies;
		InterleavedBodies ibodies;
		bool is_sbodies_actual;
	private:
		//Performs synchronization of requred range of separated bodies.
		void synchronizeSBodies(size_t lo, size_t hi);
		//Performs synchronization of requred range of interleaved bodies.
		void synchronizeIBodies(size_t lo, size_t hi);
		//Checks correspondence between required size and actual size.
		void checkSize();
		//Version, that resizes bodies and synchronizes them.
		void synchronize(bool separated);
		//Distributes bodies randomly.
		void distributeBodies_Random(size_t lo, size_t hi);
		//Distributes bodies as circle.
		void distributeBodies_Circle(size_t lo, size_t hi);
		//Performs corresponded distribution for required range.
		void distributeBodies(size_t lo, size_t hi);
	public:
		//Creates body manager with requred parameters.
		BodyManager(size_t count);
		//Returns sub-vector with interleaved version of bodies.
		inline InterleavedBodies::SubVector getInterleavedBodies()
		{
			synchronize(false);
			return InterleavedBodies::SubVector(&ibodies);
		}
		//Returns sub-vector with interleaved version of bodies.
		inline SeparatedBodies::SubVector getSeparatedBodies()
		{
			synchronize(true);
			return SeparatedBodies::SubVector(&sbodies);
		}
};
